package chapter1;

/**
 * @author Kenny
 */

public class MineSweeper {
	public static void main(String [] args)
	{
		char [][] test = new char[][] {{'*','*','.','.'},{'.','.','.','.'},{'.','*','.','.'},{'.','.','.','.'}};
	
		mineSweeper(4,4,test);
	}
	
	public static void mineSweeper(int rows, int columns, char[][]line)
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				// Check if index has a mine - i.e '*'.
				if (line[i][j] == '*')
				{
					// Retrieve positions surrounding the mine.
					int north = i - 1;
					int south = i + 1;
					int east = j + 1;
					int west = j - 1;
					 
							
					if (north > - 1)
					{
						if (line[north][j] != '*')
						{
							int value = Character.getNumericValue(line[north][j]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[north][j] = Character.forDigit(value + 1, 10);	
						}
					}
					
					if (south < columns)
					{
						if (line[south][j] != '*')
						{
							int value = Character.getNumericValue(line[south][j]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[south][j] = Character.forDigit(value + 1, 10);
						}
					}
					
					if (east < rows)
					{
						if (line[i][east] != '*')
						{
							int value = Character.getNumericValue(line[i][east]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[i][east] = Character.forDigit(value + 1, 10);	
						}
					}
					
					if (west > -1)
					{
						if (line[i][west] != '*')
						{
							int value = Character.getNumericValue(line[i][west] + 1);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[i][west] = Character.forDigit(value + 1, 10);
						}
					}
					
					// top left diagonal.
					if (west > -1 && north > - 1)
					{
						int value = Character.getNumericValue(line[north][west]);
							
							if (value == -1)
							{
								value = 0;
							}
							
						line[north][west] = Character.forDigit(value + 1, 10);
					}
					
					// top right diagonal.
					if (east < rows && north > -1)
					{
						int value = Character.getNumericValue(line[north][east]);
						
						if (value == -1)
						{
							value = 0;
						}
						
						line[north][east] = Character.forDigit(value + 1, 10);
					}
						
					// bottom left diagonal,
					if (west > -1 && south < columns)
					{
						int value = Character.getNumericValue(line[south][west]);
						
						if (value == -1)
						{
							value = 0;
						}
						
						line[south][west] = Character.forDigit(value + 1, 10);
					}
					
					// bottom right diagonal.
					if (east < rows && south < columns)
					{
						int value = Character.getNumericValue(line[south][east]);
						
						if (value == -1)
						{
							value = 0;
						}
						
						line[south][east] = Character.forDigit(value + 1, 10);
					}
					
				} else if (line[i][j] == '.') {
					line[i][j] = '0';
				}
			}
		}
		
		printResults(rows, columns, line);
	}
	
	public static void printResults(int n, int m, char[][]result)
	{
		System.out.println(n + " " + m);
		
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				System.out.print(result[i][j]);
			}
			System.out.println();
		}
	}
}
